﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;
using static YDServices.Enum.OrderEnum;

namespace YDWin.Sending
{
    public partial class AddSendingForm : AddForm
    {
        SendingService svr = new SendingService();
        CustomerService fs = new CustomerService(); 

        public T_Sending editTu = new T_Sending();
        public T_Order_Details editLi = new T_Order_Details();
        public string state = "";
        public AddSendingForm()
        {
            InitializeComponent();


            //设置日期控件格式
            this.deOperationTime.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.Mask.EditMask = "yyyy年MM月dd日";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
       
            T_Sending fact = new T_Sending();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            fact.Customer_ID = (long)cboCustomersName.EditValue;
            fact.Customer_Name = cboCustomersName.Text;
            fact.SpecName = txtSpecName.Text;
            fact.OperatintTime = (DateTime)deOperationTime.EditValue;
            fact.Quantity = decimal.Parse(nudQuantity.Text);
            fact.Weight = decimal.Parse(txtWeight.Text); 
            fact.SumWeight = decimal.Parse(nudSumWeight.Text);
            fact.UnitPrice = decimal.Parse(txtUnitPrice.Text);
            fact.TotalPrice = decimal.Parse(nudTotalPrice.Text);
            fact.Cost = decimal.Parse(txtCost.Text);
            fact.Profit = decimal.Parse(txtProfit.Text);
            fact.Memo = txtMemo.Text; 
            

            fact.Driver = txtDriver.Text;
            fact.Freight= decimal.Parse(txtFreight.Text);
            fact.IsComplete = cboIsComplete.Text.Equals("是")?true:false;


            if (!state.Equals("") && state.Equals("Add") && editLi != null)//新增
            {
                fact.OrderDetails_ID = editLi.ID; 
            }

            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            if (!state.Equals("") && state.Equals("Edit"))
            {
                 svr.EditItem(fact);
            }
            else
            {
                //如果已发完,则计算损耗比我损耗匹数
                //fact.LossRatio = decimal.Parse(txtLossRatio.Text);
                //fact.LossPiece = decimal.Parse(txtLossPiece.Text);

                svr.AddItem(fact);
            } 
            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "发货信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddSendingForm_Load(object sender, EventArgs e)
        {
            //客户
            var fDs = fs.getComboxList();
            cboCustomersName.Properties.ValueMember = "ID";   //实际要用的字段
            cboCustomersName.Properties.DisplayMember = "CustomerName";    //要显示的字段
            cboCustomersName.Properties.DataSource = fDs;
            cboCustomersName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容

          
            if (!state.Equals("") && state.Equals("Add") && editLi != null)//新增
            {
                deOperationTime.EditValue = DateTime.Now;
                nudQuantity.Text = (string.IsNullOrEmpty(editLi.SurplusQuantity.ToString()) ? "0" : editLi.SurplusQuantity.ToString());
                nudSumWeight.Text = (string.IsNullOrEmpty(editLi.SurplusWeight.ToString()) ? "0" : editLi.SurplusWeight.ToString());
                txtWeight.Text= (string.IsNullOrEmpty(editLi.Weight.ToString()) ? "0" : editLi.Weight.ToString()); 
                txtCost.Text= (string.IsNullOrEmpty(editLi.TotalCost.ToString()) ? "0" : (editLi.TotalCost/editLi.Quantity*editLi.SurplusQuantity).ToString());
                txtSpecName.Text = editLi.Spec_Name.ToString();
            }

            if (!state.Equals("") && state.Equals("Edit") && editTu != null)//编辑
            {
                cboCustomersName.Text = editTu.Customer_Name; 
                deOperationTime.EditValue = editTu.OperatintTime;
                nudQuantity.Text = editTu.Quantity.ToString();
                nudSumWeight.Text = editTu.Weight.ToString();
                txtWeight.Text = editTu.Weight.ToString();
                txtUnitPrice.Text = editTu.UnitPrice.ToString();
                nudTotalPrice.Text = editTu.TotalPrice.ToString();
                txtCost.Text = editTu.Cost.ToString();
                txtProfit.Text = editTu.Profit.ToString();
                txtMemo.Text = editTu.Memo.ToString();
                txtSpecName.Text = editTu.SpecName.ToString(); 
 
                txtDriver.Text = editTu.Driver.ToString();
                txtFreight.Text = editTu.Freight.ToString();
                cboIsComplete.Text = editTu.IsComplete==true?"是":"否";

            }


        }


        #region 数量录入后自动计算 
        private void nudQuantity_KeyUp(object sender, KeyEventArgs e)
        {
            if (decimal.Parse(nudQuantity.Text) > editLi.Quantity)
            {
                nudQuantity.Text = editLi.SurplusQuantity.ToString();
                Utils.ShowPrompt(MessageType.mtInformation, "数量超过最大库存数量!");
            }

            //总重量
            nudSumWeight.Text = (decimal.Parse(txtWeight.Text) * decimal.Parse(nudQuantity.Text)).ToString();
            //总金额
            nudTotalPrice.Text = (decimal.Parse(nudSumWeight.Text) * decimal.Parse(txtUnitPrice.Text)).ToString();
            //成本
            txtCost.Text = (editLi.TotalCost / editLi.Quantity * decimal.Parse(nudQuantity.Text)).ToString();

            //利润 
            txtProfit.Text = (decimal.Parse(nudTotalPrice.Text) - decimal.Parse(txtCost.Text)).ToString();
   
        }
        private void txtWeight_KeyUp(object sender, KeyEventArgs e)
        {
            //总重量
            nudSumWeight.Text = (decimal.Parse(txtWeight.Text) * decimal.Parse(nudQuantity.Text)).ToString();
            //总金额
            nudTotalPrice.Text = (decimal.Parse(nudSumWeight.Text) * decimal.Parse(txtUnitPrice.Text)).ToString();
            //利润 
            txtProfit.Text = (decimal.Parse(nudTotalPrice.Text) - decimal.Parse(txtCost.Text)).ToString();
        }

        private void txtUnitPrice_KeyUp(object sender, KeyEventArgs e)
        {
            //总金额
            nudTotalPrice.Text = (decimal.Parse(nudSumWeight.Text) * decimal.Parse(txtUnitPrice.Text)).ToString();
            //利润
            txtProfit.Text = (decimal.Parse(nudTotalPrice.Text) - decimal.Parse(txtCost.Text)).ToString();
        }

        private void nudTotalPrice_KeyUp(object sender, KeyEventArgs e)
        {
            //利润
            txtProfit.Text = (decimal.Parse(nudTotalPrice.Text) - decimal.Parse(txtCost.Text)).ToString();
        }



        #endregion
 
    }
}